package com.ebai.product.management.controller.system;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ebai.product.management.base.controller.WebController;
import com.ebai.product.management.base.utils.Common;
import com.ebai.product.management.base.utils.SysCode;
import com.ebai.product.management.framework.utils.security.ShiroUtils;
import com.ebai.product.management.model.entity.Dict;
import com.ebai.product.management.service.system.DictService;

import net.sf.json.JSONObject;

/**
 * 字典管理 控制跳转类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/system/dict")
public class DictController extends WebController{
	
	@Autowired
	private DictService dictService;

	@RequestMapping("/index")
	@RequiresPermissions("system:dict:view")
    public String indexPage(ModelMap modelMap) {
        return "system/dict/index";
    }

    /**
     * AJAX调用分页数据信息
     * @param response HTTP响应
     * @param page 当前页码
     * @param limit 分页条数
     * @param sort 排序方式
     * @param order 排序字段
     * @param condition 查询条件
     */
    @RequestMapping("/pageList")
    public void jsonPageList(HttpSession session, HttpServletResponse response, int page, int limit, 
    		String sort, String order, Dict condition, String parentId) {
        JSONObject json = Common.getJsonDatagrid(this.dictService.queryPage(page, limit, condition, parentId));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }
    
    /**
     * 新增或修改时的表单页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/form")
    public String formPage(ModelMap modelMap, String type) {
    	modelMap.addAttribute("dictType", type);
    	if (StringUtils.equals(type, SysCode.DICT_TYPE.PARENT)) {
    		return "system/dict/typeform";
    	}
        return "system/dict/dataform";
    }

    /**
     * 表单数据保存
     * @param response
     * @param data
     */
    @RequestMapping("/save")
    public void jsonDataSave(HttpServletResponse response, Dict data) {
        JSONObject result = Common.getJsonResult(this.dictService.saveData(data, ShiroUtils.getUser()));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 列表数据删除
     * @param response
     * @param ids
     */
    @RequestMapping("/drop")
    public void jsonDataDrop(HttpServletResponse response, String dataPid) {
        JSONObject result = Common.getJsonResult(this.dictService.dropData(dataPid));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }
}
